import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import path from "node:path";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";
import { visualizer } from "rollup-plugin-visualizer";
// https://vitejs.dev/config/
export default defineConfig(({ mode, command }) => {
  const env = loadEnv(mode, process.cwd());
  console.log(env, mode, command);
  return {
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/style/variables.scss" as *;`,
        },
      },
    },
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
      },
      extensions: [".js", ".ts", ".vue", ".json"],
    },
    plugins: [
      vue(),
      AutoImport({
        imports: ["vue", "vue-router", "pinia"], // 自动导入vue相关函数
        dirs: ["src/hooks", "src/utils", "src/store"], // 自动导入hook、工具函数、pinia本地项目的ts文件自动导入
        dts: "src/auto-imports.d.ts", // 生成自动导入类型声明文件
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        dirs: ["src/components", "src/layout"], // 本地项目的vue组件自动导入
        dts: "src/components.d.ts", // 生成组件类型声明文件
        extensions: ["vue"],
        // excludeNames: [/^Async.+/], // 排除异步组件
        resolvers: [ElementPlusResolver()], // element-plus组件自动导入 三方组件库组件自动导入
      }),
      visualizer({ open: true }), // 打包分析插件
    ],
    server: {
      proxy: {
        "/api": {
          target: "http://localhost:5008",
          changeOrigin: true,
        },
      },
    },
    build: {
      minify: "terser",
      terserOptions: {
        compress: {
          drop_console: true,
        },
      },
      assetsInlineLimit: 1024 * 3,
      rollupOptions: {
        input: {
          main: path.resolve(process.cwd(), "index.html"),
        },
        output: {
          dir: "dist",
          entryFileNames: "js/[name].js",
          chunkFileNames: "js/[name]-[hash].js",
          assetFileNames: "assets/[name]-[hash].[ext]",
          manualChunks: {
            "element-plus": ["element-plus"],
            "@wangeditor/editor-for-vue": ["@wangeditor/editor-for-vue"],
          },
        },
      },
    },
  };
});
